package ma.projet.service;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;



import ma.projet.beans.Specialite;
import ma.projet.interfaces.IDao;
import ma.projet.HibernateUtil;

public class SpecialiteService implements IDao<Specialite> {

	@Override
	public Specialite getById(int id) {
		 Session session = HibernateUtil.getSessionFactory().openSession();
         session.getTransaction().begin();
         Specialite p = (Specialite) session.get(Specialite.class, id);
         session.getTransaction().commit();
         session.close();
         return p;
	}

	@Override
	public void create(Specialite o) throws HibernateException {
		// TODO Auto-generated method stub
		Session session = null;
		Transaction tx = null;

		try {
			session = HibernateUtil.getSessionFactory().openSession();
			tx = session.beginTransaction();
			session.save(o);
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}

	@Override
	public void delete(Specialite o) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void update(Specialite o) {
		// TODO Auto-generated method stub
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Specialite> load() {
		// TODO Auto-generated method stub
		Session session=null;
		List<Specialite> liste;
	try{	
		
		
		session=HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		liste=session.createQuery("From Specialite").list();
		session.getTransaction().commit();
	
		return liste;}
	catch(HibernateException ex)
	{
		session.getTransaction().rollback();
		System.out.println("Liste Vide");
		return null;
	}
	
	}
	

}